package com.example.user.customviewdemo.drawing.xfermode;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by user on 2016/5/6.
 */
public class CircleWaterView extends View {

    private Bitmap srcBmp,dstBmp;
    private Paint paint,circlePaint,waterPaint;
    private Path path;
    private int dy,offset;


    public CircleWaterView(Context context) {
        super(context);
    }

    public CircleWaterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setElevation(10);
        }
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setColor(Color.WHITE);

        waterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        waterPaint.setColor(Color.BLUE);
        waterPaint.setStyle(Paint.Style.FILL);
        path = new Path();

        srcBmp = mkSrcBmp(600,600);
        startAnimator(700,100);


    }

    public CircleWaterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xff293134);
        dstBmp = mkDstBmp(600,600);

        canvas.drawBitmap(srcBmp,getWidth()/2-srcBmp.getWidth()/2,getHeight()/2-srcBmp.getHeight()/2,paint);
        int layerID = canvas.saveLayer(0,0,getWidth(),getHeight(),null,Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(dstBmp,getWidth()/2-dstBmp.getWidth()/2,getHeight()/2-dstBmp.getHeight()/2,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(srcBmp,getWidth()/2-srcBmp.getWidth()/2,getHeight()/2-srcBmp.getHeight()/2,paint);
        paint.setXfermode(null);
        canvas.restoreToCount(layerID);

    }

    /**
     * 创建一个圆形的srcBmp
     * @param width
     * @param height
     * @return
     */
    private Bitmap mkSrcBmp(int width,int height){
        Bitmap bm = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        c.drawCircle(width/2,height/2,width/2,circlePaint);

        return bm;
    }

    private Bitmap mkDstBmp(int width,int height){
        path.reset();
        Bitmap bm = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        path.moveTo(0,height - offset);
        path.rQuadTo(width/4,-dy,width/2,0);
        path.rQuadTo(width/4,dy,width/2,0);
        path.lineTo(width,height);
        path.lineTo(0,height);
        path.close();
        c.drawPath(path,waterPaint);

        return bm;
    }

    private void startAnimator(int offset1,int dy1){
        ValueAnimator animator = ValueAnimator.ofInt(0,offset1);
        animator.setDuration(5000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                offset = (int) animation.getAnimatedValue();
                invalidate();
            }
        });ValueAnimator animator1 = ValueAnimator.ofInt(dy1,-dy1,dy1);
        animator1.setDuration(1000);
        animator1.setRepeatCount(ValueAnimator.INFINITE);
        animator1.setInterpolator(new LinearInterpolator());
        animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                dy = (int) animation.getAnimatedValue();
                invalidate();
            }
        });

        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator,animator1);
        set.start();

    }

}
